{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "9a4b7cd4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.2ccad37385ad953e260eb7de7e22b1a7.315360000.1965042565.282335-25946558', 'expires_in': 2592000, 'session_key': '9mzdXqR2TgngJU8VQMYHjh9juokzqXVf9YcxAj3HVEpbXRbk2fVYsW+urUwOWN/70lLUrZ2HIxpV3acTpmz1Lz+DzWV7kg==', 'access_token': '24.eac4327f41e8d20529a9101d02651e92.2592000.1652274565.282335-25946558', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '50a437e07e38fdf7b88f72f314045752'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'ydC1X0R4oFwifh8yU8IFU5p9',\n",
    "    'client_secret':'setGlbwaBySbtHFSIpqR5VfES0SBUOVS'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c49a924f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.eac4327f41e8d20529a9101d02651e92.2592000.1652274565.282335-25946558'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# token获取\n",
    "token = response.json()[\"access_token\"]\n",
    "token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb7615c3",
   "metadata": {},
   "source": [
    "## 通用文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "ec6391f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64\n",
    "def everything(token,image_path):\n",
    "    url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(image_path, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = {\n",
    "        \"image\":img,\n",
    "        \"access_token\": token\n",
    "         }\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    response = requests.post(url, data=params, headers=headers)\n",
    "    return response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "e673b69f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '化粧品「花王一」女仆飞。'},\n",
       "  {'words': '最新製品情報入少了、'},\n",
       "  {'words': '夕了情報苍二覽叶末。'},\n",
       "  {'words': '盈啡斗运入'},\n",
       "  {'words': '早吧叫外'},\n",
       "  {'words': '早吕明吧外？'},\n",
       "  {'words': '入科叶上景利吕。合卡显？'},\n",
       "  {'words': '昵图网nipic.com/wxrhd'}],\n",
       " 'words_result_num': 8,\n",
       " 'log_id': 1513504554003100600}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "words_info = everything(token,\"hanyu.jpeg\")\n",
    "words_info\n",
    "## 识别成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "4841da2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '信'},\n",
       "  {'words': 'K'},\n",
       "  {'words': '行'},\n",
       "  {'words': '印'},\n",
       "  {'words': 'KI'},\n",
       "  {'words': '8'},\n",
       "  {'words': 'K'},\n",
       "  {'words': 'm'},\n",
       "  {'words': 'K”'},\n",
       "  {'words': '-《'},\n",
       "  {'words': 'H'}],\n",
       " 'words_result_num': 11,\n",
       " 'log_id': 1513504886784785299}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "words_info = everything(token,\"bosi.jpeg\")\n",
    "words_info\n",
    "## 识别成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "689a0b7a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '改字'}],\n",
       " 'words_result_num': 1,\n",
       " 'log_id': 1513505021025249323}"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "words_info = everything(token,\"hanzi.jpeg\")\n",
    "words_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5ae26a6",
   "metadata": {},
   "source": [
    "## 网络图片文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "b539e689",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64\n",
    "def everything(token,image_path):\n",
    "    url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(image_path, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = {\n",
    "        \"image\":img,\n",
    "        \"access_token\": token\n",
    "         }\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    response = requests.post(url, data=params, headers=headers)\n",
    "    return response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "49558f6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '01'},\n",
       "  {'words': '2020108'},\n",
       "  {'words': '建军节'},\n",
       "  {'words': '热烈庆祝建军节成立九十三周年'},\n",
       "  {'words': '八一建军节'},\n",
       "  {'words': '妙娇现'},\n",
       "  {'words': 'ARMY DAY'},\n",
       "  {'words': '八一建军节'},\n",
       "  {'words': '建/军/九/十/三/周/年'},\n",
       "  {'words': '大图网DAIMG.com'},\n",
       "  {'words': 'NO:2020072300077788'}],\n",
       " 'words_result_num': 11,\n",
       " 'log_id': 1513504626384027450}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_info = everything(token,\"haibao.jpeg\")\n",
    "image_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1f837de",
   "metadata": {},
   "source": [
    "## 身份证识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "fb4160dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64\n",
    "def everything(token,image_path):\n",
    "    url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(image_path, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = {\n",
    "        \"image\":img,\n",
    "        \"access_token\": token\n",
    "         }\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    response = requests.post(url, data=params, headers=headers)\n",
    "    return response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1be1601f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "84deaf48",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '中华人民共和国'},\n",
       "  {'words': '居民身份证'},\n",
       "  {'words': '签发机关一南充市公安局顺庆分局'},\n",
       "  {'words': '有效期限'},\n",
       "  {'words': '2020.09.22-2030.09.22'},\n",
       "  {'words': '红星新闻'}],\n",
       " 'words_result_num': 6,\n",
       " 'log_id': 1513504699500120049}"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id_info = everything(token,\"shenfenzheng.jpeg\")\n",
    "id_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c22ddd3c",
   "metadata": {},
   "source": [
    "## 二维码识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "9f4b3f0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64\n",
    "def everything(token,image_path):\n",
    "    url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(image_path, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = {\n",
    "        \"image\":img,\n",
    "        \"access_token\": token\n",
    "         }\n",
    "    headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    response = requests.post(url, data=params, headers=headers)\n",
    "    return response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "89869eca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'codes_result': [{'text': ['http://weixin.qq.com/r/WkyZgWfErzrQrSPi9xlI'],\n",
       "   'type': 'QR_CODE'}],\n",
       " 'codes_result_num': 1,\n",
       " 'log_id': 1513506496982299311}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code_info = everything(token,\"code.jpeg\")\n",
    "code_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68a72af1",
   "metadata": {},
   "source": [
    "## 银行卡识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "d18bb7d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64\n",
    "def everything(token,image_path):\n",
    "    url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard\"\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(image_path, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    \n",
    "    params = {\n",
    "        \"image\":img,\n",
    "        \"access_token\": token\n",
    "         }\n",
    "    hebders = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "    response = requests.post(url, data=params, headers=headers)\n",
    "    return response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "2c0a84ac",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result': {'valid_date': '09/22',\n",
       "  'bank_card_number': '6217 0038 6000 2354 304',\n",
       "  'bank_name': '建设银行',\n",
       "  'bank_card_type': 1,\n",
       "  'holder_name': ''},\n",
       " 'direction': 0,\n",
       " 'log_id': 1513508422506425478}"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bankcard_info = everything(token,\"bankcard.jpeg\")\n",
    "bankcard_info"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
